package com.example.demo.address.service;

import com.example.demo.address.repository.AddressRepository;
import com.example.demo.address.bean.AddressBean;
import com.example.demo.service.BaseService;
import com.example.demo.user.bean.UserBean;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressService extends BaseService<AddressBean,AddressRepository>{

    public  List<AddressBean> getAddListByUser(UserBean userBean){
       return repository.getAllByUserBean(userBean);
    }

    public void save(AddressBean addressBean, UserBean userBean) {
        if (addressBean.getId()!=0){
            //更新
            AddressBean temp = repository.findOne(addressBean.getId());
            temp.setUserBean(userBean);
            temp.setName(addressBean.getName());
            temp.setPhone(addressBean.getPhone());
            temp.setAdd1(addressBean.getAdd1());
            temp.setAdd2(addressBean.getAdd2());
            addressBean = temp;
        }else{
            addressBean.setUserBean(userBean);
        }
        repository.save(addressBean);
    }
}
